package com.runtime.hongyunuser.controller;

import com.runtime.hongyunuser.bean.Users;
import com.runtime.hongyunuser.service.PasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/password")
public class PasswordController {

    @Autowired
    private PasswordService passwordService;

    @PostMapping("/reset")
    public ResponseEntity<String> resetPassword(@RequestBody Map<String, String> passwords,
                                                HttpServletRequest request,
                                                HttpServletRequest req) {
        Users loginUser = (Users) request.getSession().getAttribute("loginAcc");
        Long userId = loginUser.getUserId();

        String oldPassword = passwords.get("oldPassword");
        String newPassword = passwords.get("newPassword");

        boolean success = passwordService.resetPassword(userId, oldPassword, newPassword);
        if (success) {
            req.getSession().removeAttribute("loginAcc");
            return ResponseEntity.ok("修改成功!");
        } else {
            return ResponseEntity.badRequest().body("修改失败");
        }
    }
}